package com.originskyseed.platform.user.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.user.mapper.OrgGovernmentMapper;
import com.originskyseed.platform.user.model.OrgGovernment;
import com.originskyseed.platform.user.service.IOrgGovernmentService;

/**
 * 行政区划表Service接口实现类（OSI_USER_ORG）
 * 
 * @author LiChengHuan
 */
@DubboService
@Component("orgGovernmentService")
public class OrgGovernmentServiceImpl implements IOrgGovernmentService {

	@Resource
	private OrgGovernmentMapper orgGovernmentMapper;

	@Override
	public List<OrgGovernment> selectByAreaId(String areaId) throws Exception {
		return this.orgGovernmentMapper.selectByAreaId(areaId);
	}

	/**
	 * 删除记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.orgGovernmentMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 * 
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.orgGovernmentMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(OrgGovernment record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.orgGovernmentMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public JSONObject insertSelective(OrgGovernment record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		if (StringUtils.isBlank(record.getParentCode())) {
			record.setParentCode("0");
		}
		this.orgGovernmentMapper.insertSelective(record);
		return OrgGovernmentToJSONObject(record);
	}

	/**
	 * 更新记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(OrgGovernment record) throws Exception {
		record.setUpdateTime(new Date());
		return this.orgGovernmentMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public JSONObject updateByPrimaryKeySelective(OrgGovernment record) throws Exception {
		record.setUpdateTime(new Date());
		this.orgGovernmentMapper.updateByPrimaryKeySelective(record);
		OrgGovernment orgGovernment = selectByPrimaryKey(record.getId());
		orgGovernment.setIsParent(selectExitChild(record) == true ? 1 : 0);
		return OrgGovernmentToJSONObject(orgGovernment);
	}

	/**
	 * 查询记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public OrgGovernment selectByPrimaryKey(String id) throws Exception {
		return this.orgGovernmentMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询记录转为JSONObject
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public JSONObject selectJSONObjectById(String id) throws Exception {
		return OrgGovernmentToJSONObject(this.orgGovernmentMapper.selectByPrimaryKey(id));
	}

	/**
	 * 查询列表
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<OrgGovernment> selectList(OrgGovernment record) throws Exception {
		return this.orgGovernmentMapper.selectList(record);
	}

	@Override
	public List<OrgGovernment> selectListByCountryCode(String countryCode) throws Exception {
		return this.orgGovernmentMapper.selectListByCountryCode(countryCode);
	}

	/**
	 * 分页查询列表
	 * 
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<OrgGovernment> selectListByPage(Map<String, Object> params, BasePage<OrgGovernment> page) throws Exception {
		params.put("page", page);
		List<OrgGovernment> list = this.orgGovernmentMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 根据父级code获取树
	 *
	 * @return
	 * @throws Exception
	 */
	@Override
	public JSONArray selectGovernmentByParentCode(String parentCode) throws Exception {
		OrgGovernment orgGovernment = new OrgGovernment();
		orgGovernment.setParentCode(parentCode);
		List<OrgGovernment> OrgGovernmentList = this.orgGovernmentMapper.selectTreeByParentCode(orgGovernment);
		return listChangeJsonArray(OrgGovernmentList);
	}

	/**
	 * 根据等级id获取树
	 *
	 * @return
	 * @throws Exception
	 */
	@Override
	public JSONArray selectGovernmentByLevel(String levelId) throws Exception {
		OrgGovernment orgGovernment = new OrgGovernment();
		orgGovernment.setLevelId(levelId);
		// 获取所有字典列表
		List<OrgGovernment> OrgGovernmentList = this.orgGovernmentMapper.selectTreeByParentCode(orgGovernment);
		return listChangeJsonArray(OrgGovernmentList);
	}

	/**
	 * 获取树
	 *
	 * @return
	 * @throws Exception
	 */
	@Override
	public JSONArray selectUserOrgTree() throws Exception {
		// 获取所有字典列表
		List<OrgGovernment> OrgGovernmentList = this.orgGovernmentMapper.selectAllUserOrg();
		return listChangeJsonArray(OrgGovernmentList);
	}

	@Override
	public Boolean selectExitChild(OrgGovernment OrgGovernment) throws Exception {
		return this.orgGovernmentMapper.selectExitChild(OrgGovernment);
	}

	/**
	 * 判断编码是否存在
	 * 
	 * @param orgGovernment
	 * @return
	 * @throws Exception
	 */
	@Override
	public Boolean existCode(OrgGovernment orgGovernment) throws Exception {
		return this.orgGovernmentMapper.existCode(orgGovernment);
	}

	@Override
	public String selectNameByCode(String code) {
		return this.orgGovernmentMapper.selectNameByCode(code);
	}

	/**
	 * 集合改变成json数组
	 * 
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public JSONArray listChangeJsonArray(List<OrgGovernment> list) throws Exception {
		JSONArray result = new JSONArray();
		// 追加字典树节点
		for (OrgGovernment orgGovernment : list) {
			JSONObject jsonObject = OrgGovernmentToJSONObject(orgGovernment);
			result.add(jsonObject);
		}
		return result;
	}

	/**
	 * 转为JSONObject
	 * 
	 * @param orgGovernment
	 * @return
	 */
	private JSONObject OrgGovernmentToJSONObject(OrgGovernment orgGovernment) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("id", orgGovernment.getId());
		jsonObject.put("parentCode", orgGovernment.getParentCode());
		jsonObject.put("countryCode", orgGovernment.getCountryCode());
		jsonObject.put("code", orgGovernment.getCode());
		jsonObject.put("status", orgGovernment.getStatus());
		jsonObject.put("sequence", StringUtils.isBlank(orgGovernment.getSequence()) ? " " : orgGovernment.getSequence());
		if (orgGovernment.getIsParent() != null && orgGovernment.getIsParent() > 0) {
			jsonObject.put("isParent", true);
		} else {
			jsonObject.put("isParent", false);
		}
		jsonObject.put("levelId", orgGovernment.getLevelId());
		jsonObject.put("name", orgGovernment.getName());
		jsonObject.put("open", false);
		return jsonObject;
	}
}